<?php
require_once "HTTP/Upload.php";
require_once 'Image/Transform.php';

$objCarForm = new BM_PHPForm();
$objCarForm->UseLocator();
$objCarForm->SetIndent("		");

$arrOptions = CarDao::findAllOptions();

if ($objCarForm->IsPost()) {
//	$objCarForm->ChEmptiness("make","Make is empty.");
//	$objCarForm->ChMinLength("make","Make must be more than 2 chars.", 3);
//	$objCarForm->ChEmptiness("model","Model is empty.");
//	$objCarForm->ChEmptiness("price", "Price is empty.");
//	$objCarForm->ChValidChrs("price", "Price Must Be Number or [A$,.]", "0123456789A$,.");
	
	if ($objCarForm->IsValid()) {
		$upload = new HTTP_Upload("en");
		$upload->setChmod(0644);
        $it = Image_Transform::factory('GD');
		$filePhoto = $upload->getFiles('photo');
		$filePic1 = $upload->getFiles('pic1');
		$filePic2 = $upload->getFiles('pic2');
		$filePic3 = $upload->getFiles('pic3');
		$filePic4 = $upload->getFiles('pic4');
		if ($filePhoto->isValid()) {
			$filePhoto->setName("uniq");
	        $dest_photo = $filePhoto->moveTo("img/cars/");
	        $photo = $filePhoto->getProp('name');
	        $it->load("img/cars/$photo");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$photo);
		}
		if ($filePic1->isValid()) {
			$filePic1->setName("uniq");
			$dest_pic1 = $filePic1->moveTo("img/cars/");
			$pic1 = $filePic1->getProp('name');
	        $it->load("img/cars/$pic1");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$pic1);
		}
		if ($filePic2->isValid()) {
			$filePic2->setName("uniq");
			$dest_pic2 = $filePic2->moveTo("img/cars/");
			$pic2 = $filePic2->getProp('name');
	        $it->load("img/cars/$pic2");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$pic2);
		}
		if ($filePic3->isValid()) {
			$filePic3->setName("uniq");
			$dest_pic3 = $filePic3->moveTo("img/cars/");
			$pic3 = $filePic3->getProp('name');
	        $it->load("img/cars/$pic3");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$pic3);
		}
		if ($filePic4->isValid()) {
			$filePic4->setName("uniq");
			$dest_pic4 = $filePic4->moveTo("img/cars/");
			$pic4 = $filePic4->getProp('name');
	        $it->load("img/cars/$pic4");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$pic4);
		}
		
		$entCar = new Car(0,$objCarForm->arrPostData['make'],
							$objCarForm->arrPostData['model'], 
							$objCarForm->arrPostData['price'],
							$objCarForm->arrPostData['color'],$photo);
		$entCar->setPics($pic1,$pic2,$pic3,$pic4);
		$entCar->setDetails($objCarForm->arrPostData['vehicle'],$objCarForm->arrPostData['door'],
							$objCarForm->arrPostData['cylind'],$objCarForm->arrPostData['fuel'],
							$objCarForm->arrPostData['gear'],$objCarForm->arrPostData['gearbox'],
							$objCarForm->arrPostData['odometer'],$objCarForm->arrPostData['rego'],
							$objCarForm->arrPostData['stock'],$objCarForm->arrPostData['utime'],
							$objCarForm->arrPostData['vin'],$objCarForm->arrPostData['comments']);					
		$carId = CarDao::save($entCar);
		if (! $carId == 0) {
			$options = array();
			foreach($arrOptions as $option){
				$optionId = $option['id'];
				if(isset($objCarForm->arrPostData[$optionId])){
					$options[]=$optionId;
				}
			}
			if (!sizeof($options) == 0){
				CarDao::saveOptions($carId,$options);
			}
			header("Location:../$www/index_admin.php?entity=car&action=list&msg=infSaved");
		}else {
			$objCarForm->SetError("submit","Mysql Error, data didn't saved !.");
		}
	}
}

print $objCarForm->InfMsgs();
print $objCarForm->ErrMsgs();

print $objCarForm->Open("carform","Car Registration Form");
print $objCarForm->HiddenField("action" ,"$action");
print $objCarForm->HiddenField("entity","$entity");

print $objCarForm->TextBox("make"  ,"Make:" ,"");
print $objCarForm->TextBox("model" ,"Model:","");
print $objCarForm->TextBox("price" ,"Price:","");
print $objCarForm->TextBox("color" ,"Color:","");
print $objCarForm->File("photo","Main Picture:");
print $objCarForm->File("pic1","Picture1:");
print $objCarForm->File("pic2","Picture2:");
print $objCarForm->File("pic3","Picture3:");
print $objCarForm->File("pic4","Picture4:");
print $objCarForm->TextBox("vehicle" ,"Vehicle:","");
print $objCarForm->TextBox("door" ,"Doors:","");
print $objCarForm->TextBox("cylind" ,"Cylinders:","");
print $objCarForm->TextBox("fuel" ,"Fuel Type:","");
print $objCarForm->TextBox("gear" ,"Gears:","");
print $objCarForm->TextBox("gearbox" ,"Gearbox:","");
print $objCarForm->TextBox("odometer" ,"Odometer:","");
print $objCarForm->TextBox("rego" ,"Rego:","");
print $objCarForm->TextBox("stock" ,"Stock:","");
//print $objCarForm->TextBox("utime" ,"Last-Updated:","");
print $objCarForm->TextBox("vin" ,"VIN:","");
print $objCarForm->TextArea("comments","Comments:","");
print "&nbsp;&nbsp;&nbsp;&nbsp;Vehicle Options:";
print "<hr/>";
foreach ($arrOptions as $option) {
	$arrCheckbox[] = $objCarForm->CheckBox2($option['id'],$option['name'],array(),true,$option['id']);
}
?>
<table>
<?php 
for ($i=0; $i<count($arrCheckbox); $i+=3) {
?>
<tr>
    <td><?php echo $arrCheckbox[$i]?></td>
    <td><?php echo $arrCheckbox[$i+1]?></td>
    <td><?php echo $arrCheckbox[$i+2]?></td>
</tr>
<?php	
}
?>
</table>
<?php
print $objCarForm->SubmitButton("submit", "Register", array(), "submit");
print $objCarForm->Close();

?>